﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Windows.Forms.Integration;
using System.Diagnostics;

namespace TFhisto
{
    /// <summary>
    /// Interaction logic for WorkItemWindow.xaml
    /// </summary>
    public partial class WorkItemWindow : Window
    {
        WorkItemStore _workItemStore;
        Dictionary<int, TabItem> _workItemTabs;

        public WorkItemWindow()
        {
            InitializeComponent();
        }

        public WorkItemWindow(WorkItem workItem)
            : this()
        {
            _workItemTabs = new Dictionary<int, TabItem>();
            _workItemStore = workItem.Store;
            ShowWorkItem(workItem);
        }

        /// <summary>
        /// Shows a work item in a tab.
        /// 
        /// If the workitem is already present in a previously created tab it that tab is made visible.
        /// 
        /// Otherwise a new tab is created with the workitem requested
        /// </summary>
        /// <param name="workItemId"></param>
        internal void ShowWorkItem(int workItemId)
        {
            WorkItem newWorkItem;

            if (ActivateTab(workItemId)) return;

            newWorkItem = _workItemStore.GetWorkItem(workItemId);

            ShowWorkItem(newWorkItem);

        }

        /// <summary>
        /// Makes visible the tab that contains the workitem. 
        /// 
        /// If there is no tab with the wanted work item false is returned
        /// </summary>
        /// <param name="workItemId"></param>
        /// <returns>True if there is a tab with the wanted workitem id false otherwise</returns>
        private bool ActivateTab(int workItemId)
        {
            if (_workItemTabs.ContainsKey(workItemId))
            {
                tabWorkItems.SelectedItem = _workItemTabs[workItemId];
                return true;
            }
            return false;
        }

        /// <summary>
        /// Creates a new tab and displays the work item in it
        /// </summary>
        /// <param name="workItem"></param>
        private void ShowWorkItem(WorkItem workItem)
        {
            WorkItemFormControl wrkItemFormControl;
            WindowsFormsHost winFrmWorkItemControlHost;
            TabItem tabItem;

            tabItem = new TabItem();
            tabItem.Header = workItem.Type.Name + ": " + workItem.Id.ToString();

            tabWorkItems.Items.Add(tabItem);

            wrkItemFormControl = new WorkItemFormControl();
            wrkItemFormControl.Dock = DockStyle.Fill;

            wrkItemFormControl.ServiceProvider = new WorkItemEditorServiceProvider(this);

            wrkItemFormControl.Item = workItem;
            wrkItemFormControl.BackColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.Control);

            winFrmWorkItemControlHost = new WindowsFormsHost();

            winFrmWorkItemControlHost.Child = wrkItemFormControl;
            winFrmWorkItemControlHost.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            winFrmWorkItemControlHost.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            winFrmWorkItemControlHost.Margin = new Thickness(5, 5, 5, 5);


            tabItem.Content = winFrmWorkItemControlHost;

            tabItem.Tag = workItem;

            _workItemTabs.Add(workItem.Id, tabItem);

            ActivateTab(workItem.Id);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SaveActiveWorkItem();                
            }
            catch (Exception exception)
            {
                System.Windows.MessageBox.Show("Error saving work item " + exception.Message);
            }
        }

        private void SaveActiveWorkItem()
        {
            Debug.Assert(tabWorkItems.SelectedItem != null);
            Debug.Assert(((TabItem)tabWorkItems.SelectedItem).Tag != null);

            ((WorkItem)((TabItem)tabWorkItems.SelectedItem).Tag).Save();                
        }


    }
}
